package com.hhl.flower.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hhl.flower.mapper.CollectionMapper;
import com.hhl.flower.pojo.entity.Collections;
import com.hhl.flower.pojo.entity.Flower;
import com.hhl.flower.service.CollectionService;
import com.hhl.flower.service.FlowerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
* @author Liubuzhu
* @description 针对表【collection】的数据库操作Service实现
* @createDate 2024-06-12 14:38:31
*/
@Service
public class CollectionServiceImpl extends ServiceImpl<CollectionMapper, Collections>
    implements CollectionService{

	@Autowired
	FlowerService flowerService;
	@Autowired
	CollectionMapper collectionMapper;

	public List<Flower> listCollectionFlower(Integer userId) {
		// 查询收藏的花
		QueryWrapper<Collections> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("u_id", userId);
		List<Collections> collectionsList = collectionMapper.selectList(queryWrapper);

		// 获取花的详情信息
		List<Flower> flowerList = new ArrayList<>();
		for (Collections collections : collectionsList) {
			Flower flower = flowerService.getById(collections.getFId());

			flowerList.add(flower);
		}

		return flowerList;
	}

	public void likeOrCancel(Boolean status, Integer flowerId, Integer userId){
		if(!status){
			// 取消收藏
			QueryWrapper<Collections> queryWrapper = new QueryWrapper<>();
			queryWrapper.eq("u_id",userId);
			queryWrapper.eq("f_id",flowerId);
			collectionMapper.delete(queryWrapper);
		} else {
			// 收藏
			Collections collections = new Collections();
			collections.setFId(flowerId);
			collections.setUId(userId);
			collectionMapper.insert(collections);
		}
	}

	public Boolean isCollection(Integer flowerId, Integer userId) {
		QueryWrapper<Collections> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("u_id",userId);
		queryWrapper.eq("f_id",flowerId);
		List<Collections> collectionsList = collectionMapper.selectList(queryWrapper);

		return !collectionsList.isEmpty();
	}

}




